﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;

namespace DevHub.WindowsApp.Pages.Converter;

public partial class BaseConverterView : UserControl
{
    public BaseConverterView()
    {
        InitializeComponent();
    }

    private async void DecimalClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        DecimalBox.Text = await top?.Clipboard?.GetTextAsync()!;
    }

    private async void HexadecimalClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        HexadecimalBox.Text = await top?.Clipboard?.GetTextAsync()!;
    }

    private async void BinaryClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        BinaryBox.Text = await top?.Clipboard?.GetTextAsync()!;
    }

    private async void OctalClick(object? sender, RoutedEventArgs e)
    {
        var top = TopLevel.GetTopLevel(this);
        OctalBox.Text = await top?.Clipboard?.GetTextAsync()!;
    }

    private void DecimalKeyDown(object? sender, KeyEventArgs e)
    {
        if (string.IsNullOrEmpty(DecimalBox.Text)) return;
        if (long.TryParse(DecimalBox.Text.Replace(",", ""), out var l))
        {
            ConvertNum(l);
        }
        else
        {
            Bar.Message = "不是标准的十进制字符";
            Bar.IsOpen = true;
        }
    }

    private void HexadecimalKeyDown(object? sender, KeyEventArgs e)
    {
        if (string.IsNullOrEmpty(HexadecimalBox.Text)) return;
        if (long.TryParse(HexadecimalBox.Text.Replace(" ", ""),
                NumberStyles.HexNumber,
                CultureInfo.InvariantCulture,
                out var l))
        {
            ConvertNum(l);
        }
        else
        {
            Bar.Message = "不是标准的十六进制字符";
            Bar.IsOpen = true;
        }
    }

    private void BinaryKeyDown(object? sender, KeyEventArgs e)
    {
        if (string.IsNullOrEmpty(BinaryBox.Text)) return;
        if (long.TryParse(BinaryBox.Text.Replace(" ", ""), out var l))
        {
            ConvertNum(l);
        }
        else
        {
            Bar.Message = "不是标准的二进制字符";
            Bar.IsOpen = true;
        }
    }

    private void OctalKeyDown(object? sender, KeyEventArgs e)
    {
        if (string.IsNullOrEmpty(OctalBox.Text)) return;
        if (long.TryParse(OctalBox.Text.Replace(" ", ""), out var l))
        {
            ConvertNum(l);
        }
        else
        {
            Bar.Message = "不是标准的八进制字符";
            Bar.IsOpen = true;
        }
    }

    private void ConvertNum(long l)
    {
        var hex = DataSwitch.IsChecked!.Value ? BinaryCon(Convert.ToString(l, 16)) : Convert.ToString(l, 16);
        HexadecimalBox.Text = HexSwitch.IsChecked!.Value ? hex.ToUpper() : hex;
        BinaryBox.Text = DataSwitch.IsChecked!.Value ? BinaryCon(Convert.ToString(l, 2)) : Convert.ToString(l, 2);
        DecimalBox.Text = DataSwitch.IsChecked!.Value ? l.ToString("N0") : Convert.ToString(l, 10);
        OctalBox.Text = DataSwitch.IsChecked!.Value ? OctalCon(Convert.ToString(l, 8)) : Convert.ToString(l, 8);
    }

    private string BinaryCon(string input)
    {
        var regex = new Regex("(.{4})", RegexOptions.RightToLeft);
        return regex.Replace(input, match => " " + match.Value);
    }

    private string OctalCon(string input)
    {
        var regex = new Regex("(.{3})", RegexOptions.RightToLeft);
        return regex.Replace(input, match => " " + match.Value);
    }
}